//
//  BookStackTableHeaderView.m
//  ProjectDemo
//
//  Created by IOS3 on 2018/11/1.
//  Copyright © 2018 IOS3. All rights reserved.
//

#import "BookStackTableHeaderView.h"
#import <SDCycleScrollView.h>


@interface BookStackTableHeaderView () <UICollectionViewDataSource, UICollectionViewDelegate, SDCycleScrollViewDelegate>

@property (nonatomic, strong) SDCycleScrollView *bannerView;
@property (nonatomic, strong) UICollectionView *collectionView;

@end

@implementation BookStackTableHeaderView

- (instancetype)init {
    self = [super init];
    if (self) {
        [self initializes];
        

    }
    return self;
}



- (void)initializes {
    UICollectionViewFlowLayout *flowLayout = [UICollectionViewFlowLayout new];
    flowLayout.itemSize = CGSizeMake(100, 100);
    flowLayout.minimumLineSpacing = 0;
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
    self.collectionView.showsVerticalScrollIndicator = NO;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = UIColor.whiteColor;
    self.bannerView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectZero delegate:self placeholderImage:nil];

    [self addSubview:self.collectionView];
    [self addSubview:self.bannerView];
    [self.collectionView registerClass:[BookStackTableHeaderCollectionCell class] forCellWithReuseIdentifier:NSStringFromClass([BookStackTableHeaderCollectionCell class])];
    
}

- (void)layoutSubviews {
    [super layoutSubviews];
    [self.bannerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(self);
        make.height.mas_equalTo(180);
    }];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.mas_equalTo(self);
        make.top.mas_equalTo(self.bannerView.mas_bottom);
    }];
}

#pragma mark - collectionView
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 5;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    BookStackTableHeaderCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([BookStackTableHeaderCollectionCell class]) forIndexPath:indexPath];
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (self.subjectSelected) {
        self.subjectSelected(indexPath.row);
    }
}

#pragma mark - SDCyleScrollView
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index {
    if (self.bannerSelected) {
        self.bannerSelected(index);
    }
}



@end


@interface BookStackTableHeaderCollectionCell ()
@property (nonatomic, strong) UIButton *button;
@end

@implementation BookStackTableHeaderCollectionCell

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        
        self.button = [[UIButton alloc] init];
        [self.button setTitle:@"title" forState:UIControlStateNormal];
        
        [self.button setImage:[UIImage imageWithColor:[UIColor randomColor] size:CGSizeMake(30, 30)] forState:UIControlStateNormal];
        
        self.button.backgroundColor = [UIColor randomColor];
        
        
        [self.contentView addSubview:self.button];
        [self.button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(self.contentView);
        }];
        
        [self.button setButtonImageTitleStyle:ButtonImageTitleStyleTop padding:5];
    }
    return self;
}

@end
